package com.example.demo2.controller;
import com.example.demo2.mapper.EquipmentMapper;
import com.example.demo2.pojo.PageBean;
import com.example.demo2.pojo.data.CategoryDO;
import com.example.demo2.pojo.data.RepairDO;
import com.example.demo2.pojo.data.SupplierDO;
import com.example.demo2.pojo.vo.CategoryVO;
import com.example.demo2.pojo.vo.EquipmentListVO;
import com.example.demo2.pojo.vo.SupplierVO;
import com.example.demo2.service.CategoryService;
import com.example.demo2.service.SupplierService;
import com.example.demo2.utils.Result;
import com.example.demo2.pojo.vo.EquipmentVO;
import com.example.demo2.service.EquipmentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/** 
 * <br/>
 * Created by weidong on 2020/02/18
 */
@Controller
public class EquipmentController {

	@Resource
	private EquipmentService equipmentService;
	@Resource
	private CategoryService categoryService;
	@Resource
	private SupplierService supplierService;
	@Resource
	private EquipmentMapper equipmentMapper;

	/**
	 *
	 * @param id  前端获取id值，根据id删除数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/equipment/delete/{id}")
	@ResponseBody
	public Result delete(@PathVariable("id") Integer id) throws Exception {
		return equipmentService.deleteById(id);
	}

	/**
	 *
	 * @param currentPage,  @RequestParam(defaultValue = "1")，如果传值为空，默认值为1
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/equipment_list")
	public String equipment(String searchname, Integer searchnumb,String searchtype,String searchstatus, @RequestParam(defaultValue = "1") Integer currentPage, Model model) throws Exception {
		EquipmentVO equipmentVO = new EquipmentVO();
		equipmentVO.setName(searchname);
		equipmentVO.setNumber(searchnumb);
		equipmentVO.setCName(searchtype);
		equipmentVO.setStatus(searchstatus);
		equipmentVO.setCurrentPage(currentPage);
		PageBean pageBean = equipmentService.listPage(equipmentVO);
		Integer count = equipmentMapper.count(equipmentVO);
		Result result=new Result();
		result.setMsg("共有:"+count+"台设备");
		model.addAttribute("title",result);
		model.addAttribute("pageBean", pageBean);
		model.addAttribute("query", equipmentVO);
		return "equipment_list";
	}

	@RequestMapping("/equipment_list/categorylist")
	@ResponseBody
	public Result categoryList() throws Exception {

		CategoryVO categoryVO = new CategoryVO();
		return categoryService.list(categoryVO);
	}
	@RequestMapping("/equipment_list/supplierlist")
	@ResponseBody
	public Result supplierList() throws Exception {
		SupplierVO supplierVO = new SupplierVO();
		return supplierService.list(supplierVO);
	}


	@RequestMapping(value = "/equipment/add", method = RequestMethod.POST)
	@ResponseBody
	//添加
	public Result addPerson(@RequestBody EquipmentListVO equipmentListVO) throws Exception {
		EquipmentVO equipmentVO = new EquipmentVO();
		CategoryDO cdata=categoryService.findCategoryName(equipmentListVO.getC_name());
		SupplierDO sdata=supplierService.findSupplierName(equipmentListVO.getS_name());
		equipmentVO.setCategoryId(equipmentListVO.getC_name());
		equipmentVO.setSId(equipmentListVO.getS_name());
		equipmentVO.setName(equipmentListVO.getName());
		equipmentVO.setCName(cdata.getType());
		equipmentVO.setStatus(equipmentListVO.getStatus());
		equipmentVO.setNumber(equipmentListVO.getNumber());
		equipmentVO.setBuydate(equipmentListVO.getBuydate());
		equipmentVO.setParameter(equipmentListVO.getParameter());
		equipmentVO.setPrice(equipmentListVO.getPrice());
		equipmentVO.setSName(sdata.getEName());
		Result result = equipmentService.add(equipmentVO);
		return result;
	}


	@RequestMapping(value = "/equipment/updateById", method = RequestMethod.POST)
	@ResponseBody
	//更新
	public Result updateEquipment(String name1, Integer classid, BigDecimal price, String deadline, Integer number , String status, String parameter, Integer s_name,Integer id) throws Exception {
		EquipmentVO equipmentVO=new EquipmentVO();
		equipmentVO.setName(name1);
		equipmentVO.setId(id);
		equipmentVO.setPrice(price);
		equipmentVO.setBuydate(deadline);
		equipmentVO.setNumber(number);
		equipmentVO.setStatus(status);
		equipmentVO.setParameter(parameter);
		CategoryDO categoryDO=categoryService.findCategoryName(classid);
		SupplierDO supplierDO=supplierService.findSupplierName(s_name);
		equipmentVO.setCName(categoryDO.getType());
		equipmentVO.setSName(supplierDO.getEName());
		Result result = equipmentService.updateById(equipmentVO);
		return result;
	}




	@RequestMapping("/equipment/findById/{id}")
	@ResponseBody
	//更新回显
	public Result findAll(@PathVariable("id") Integer id) throws Exception {
		return equipmentService.findById(id);
	}



}